package
{
	import data.CaptionTxt;
	import data.Globals;
	
	import events.EventType;
	import events.GameDispatherEvent;
	
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.SharedObject;
	
	import interfaces.ITowerWar;
	
	import manager.LevelManange;
	import manager.MapConfig;
	
	import view.AlertView;
	import view.AnimationView;
	import view.CaptionView;
	import view.GameView;
	import view.LoginView;
	
	/**
	 *@author CrazyChen 
	 */	
	[SWF(frameRate="30",width="640",height="480")]
	public class TowerWar extends Sprite implements ITowerWar
	{
		private var gameView:GameView;
		
		private var loginView:LoginView;
		
		/**
		 * 是否是登录界面进入关卡 
		 */
		private var isLoginEnter:Boolean = true;
		
		public var _4399_function_store_id:String = '3885799f65acec467d97b4923caebaae';   
		//======此代码为api通用代码，在所有api里面只需加一次========/ 
		public static var _4399_function_gameList_id:String = "944c23f5e64a80647f8d0f3435f5c7a8";   
		
		public static var serviceHold:* = null;
		public function setHold(hold:*):void{  
			serviceHold = hold;
		} 

		public function TowerWar()
		{
			Globals.towerWar = this;
			if(stage){
				addToStage();
			}else{
				this.addEventListener(Event.ADDED_TO_STAGE,addToStage);
			}
		}
		
		protected function addToStage(event:Event=null):void
		{
			Globals.stage_width = this.stage.stageWidth;
			Globals.stage_height = this.stage.stageHeight;
			
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.removeEventListener(Event.ADDED_TO_STAGE,addToStage);
			loginView = new LoginView();
			this.addChild(loginView);
			GameDispatherEvent.getInstance();//初始化事件管理器
			
			LevelManange.getInstance().userLevel = getUserLevel();
			
			this.addEventListener(MouseEvent.MOUSE_UP,mouse_up);
		}		
		
		protected function mouse_up(event:MouseEvent):void
		{
			GameDispatherEvent.dispatchEvent(EventType.MOUSE_UP);
		}
		
		public function startGame():void{
			gameUpLevel();
		}
		
		public function returnLogin():void{
			this.removeChild(gameView);
			this.addChild(loginView);
		}
		
		public function gameUpLevel(isLoginEnter:Boolean = true):void{
			saveUserLevel();
			if(!isLoginEnter){
				loginView.unlockNext();
				gameView.showAlert("成功狙击日军一个小队，其他阵地需要支援，是否前去支援！！！",alertCallback,true,true);
				recommend();
			}else{
				loaderConfig();
			}
		}
		
		/**
		 *通关 
		 */
		public function gameWin():void{
			gameView.showAlert("1945年8月15日，日本天皇宣布无条件投降！",returnLogin,true,false);
			recommend();
		}
		
		private function recommend():void{
			if(serviceHold){     
				serviceHold.showGameList();
			} 
		}
		
		/**
		 *闯关失败 
		 */
		public function throughFail():void{
			isPauseGame = true;
			Globals.gameView.showAlert("日军攻破了你的城防！是否重新再来？",failCall,true,true);
			recommend();
		}
		
		private function failCall(shouse:String):void
		{
			if(shouse == AlertView.YES){
				gameUpLevel();
			}else{
				returnLogin();
			}
		}
		
		private function loaderConfig():void{
			MapConfig.getInstance().loaderComplete = mapComplete;
			MapConfig.getInstance().loadXml(LevelManange.getInstance().curLevel+"");
		}
		
		private function mapComplete():void
		{
			if(gameView)gameView.resetView();
			isPauseGame = false;
			if(!gameView)gameView = new GameView();
			this.addChild(gameView);		
			gameView.playAnimation(AnimationView.COUNT_DOWN,gameView.start);
		}
		
		private function alertCallback(shouse:String):void{
			if(shouse == AlertView.YES){
				loaderConfig();
			}else{
				returnLogin();
				isLoginEnter = true;
			}
		}
		
		private function loop(event:Event):void
		{
			GameDispatherEvent.dispatchEvent(EventType.LOOP);
		}
		
		public function set isPauseGame(value:Boolean):void
		{
			if(value){
				this.removeEventListener(Event.ENTER_FRAME,loop);
			}else{
				this.addEventListener(Event.ENTER_FRAME,loop);
			}
		}
		
		private function saveUserLevel():void{
			var share:SharedObject = SharedObject.getLocal('crazychen');
			share.client = this;
			share.data.user = LevelManange.getInstance().userLevel;
			share.flush();
		}
		
		private function getUserLevel():int{
			var share:SharedObject = SharedObject.getLocal('crazychen');
			return share.data.user || 1;
		}

		
	}
}